package upm.pb4agent.team8.Common;

import java.awt.Point;
import java.sql.Struct;

import upm.pb4agent.common.Constant;
import upm.pb4agent.ontology.Row;
import upm.pb4agent.team8.Common.PB4Utils.MsgTypes;

public class PB4PhysicsUtils
{
	public static float getImpulseValue(int impulseOrdinal)
	{
		if ( Constant.Impulse.Weak.ordinal() == impulseOrdinal )
			return PB4Utils.PropertyReader.GetFloatKey("impulse_weak");
		else if ( Constant.Impulse.Medium.ordinal() == impulseOrdinal )
			return PB4Utils.PropertyReader.GetFloatKey("impulse_medium");
		else if ( Constant.Impulse.Strong.ordinal() == impulseOrdinal )
			return PB4Utils.PropertyReader.GetFloatKey("impulse_strong");
		else
			return 1;
	}

	public static float CalculateTiredness( float previousTiredness, float impulse, float resistance )
	{
	    float result = 0.0F;
	    result = previousTiredness + impulse * (1.0F - resistance);

	    if (result > 1.0F)
	    {
	      result = 1.0F;
	    }
	    return result;
	}

	public static int CalculateMovementX( String oarID, float strength, float tiredness, float impulse )
    {
      int movement = 0;
      float oarCoef = 0;
      
      if ( oarID.equals("1") || oarID.equals("2") )
        oarCoef = PB4Properties.BoatAttributes.FrontOarXCoef;
      else if ( oarID.equals("3") || oarID.equals("4") ) 
        oarCoef = PB4Properties.BoatAttributes.BackOarXCoef;

      PB4Utils.Console.WriteLine("CalculateMovementX "+ Float.toString(impulse) +"," + Float.toString(strength) +"," + Float.toString(tiredness) + "," + Float.toString(oarCoef), MsgTypes.Low);
      
      movement = (int)Math.ceil(impulse * (strength - tiredness) * oarCoef);
      return movement;
    }

	public static int CalculateMovementY( String oarID, float strength, float tiredness, float impulse, int direction )
    {
      int movement = 0;
      float oarCoef = 0;

      if ( oarID.equals("1") || oarID.equals("2") )
          oarCoef = PB4Properties.BoatAttributes.FrontOarYCoef;
      else if ( oarID.equals("3") || oarID.equals("4") ) 
          oarCoef = PB4Properties.BoatAttributes.BackOarYCoef;

      PB4Utils.Console.WriteLine("CalculateMovementY "+ Float.toString(impulse) +"," + Float.toString(strength) +"," + Float.toString(tiredness), MsgTypes.Low);      
      movement = (int)Math.ceil(impulse * (strength - tiredness) * oarCoef);
      return movement;
    }

	public static Point CalculateRealRow (Row row, float strength, float tiredness, float impulse, int direction)
    {	
	    int directionFactorX = PB4PhysicsUtils.CalculateMovementX( row.getOarID(),strength, tiredness, impulse );
	    int directionFactorY = PB4PhysicsUtils.CalculateMovementY( row.getOarID(),strength, tiredness, impulse, direction );

	    PB4Utils.Console.WriteLine("CalculateRealRow "+ Integer.toString(directionFactorX) +"," + Integer.toString(directionFactorY), MsgTypes.Low);
	    float x = impulse * (strength - tiredness) * PB4Properties.BoatAttributes.oar_x_coef * directionFactorX;
	    float y = impulse * (strength - tiredness) * PB4Properties.BoatAttributes.oar_y_coef * directionFactorY;
	    PB4Utils.Console.WriteLine("CalculateRealRow Final"+ Float.toString(x) +"," + Float.toString(y), MsgTypes.Low );
	    
	    Point result = new Point((int)Math.ceil(x), (int)Math.ceil(y));
	    return new Point(1,1);
	  }
}
